package jjn.round2;

import commons.ListNode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Jiang Jining
 * @since 2023-06-11 9:23
 */
public class LeetCode1171_RemoveZeroSumSubLists {
    public ListNode removeZeroSumSublists(ListNode head) {
        Map<Integer, ListNode> map = new HashMap<>();
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode node = dummy;
        int sum = 0;
        while (node != null) {
            sum += node.val;
            map.put(sum, node);
            node = node.next;
        }
        sum = 0;
        node = dummy;
        while (node != null) {
            sum += node.val;
            ListNode listNode = map.get(sum);
            node.next = listNode.next;
            node = node.next;
        }
        return dummy.next;
    }
    
}
